package com.hdd.hll.modules.order.service.impl;


import com.hdd.hll.modules.order.bean.PoundOrder;
import com.hdd.hll.modules.order.bean.PoundUpdateOrder;
import com.hdd.hll.modules.order.mapper.PoundMapper;
import com.hdd.hll.modules.order.service.PoundService;
import com.hdd.hll.modules.order.web.dto.Pound2Dto;
import com.hdd.hll.modules.order.web.dto.PoundDto;
import com.hdd.hll.modules.order.web.dto.PoundUpdateDto;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@Transactional
public class PoundServiceImpl implements PoundService {


    @Autowired
    RedissonClient redissonClient;

    @Autowired
    PoundMapper poundMapper;

    @Override
    public List<PoundOrder> findPounds(Pound2Dto dto){
        RLock lock = redissonClient.getLock("Pound"+1);
        boolean res = false;
        try {
           res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                List<PoundOrder>  truckTransports = poundMapper.findPoundsByPlanId(dto);
                return truckTransports;
            } finally {
                lock.unlock();
            }
        }
        return null;

    }

    @Override
    public List<PoundOrder> findPounds(PoundDto dto){
        RLock lock = redissonClient.getLock("Pound"+2);
        boolean res = false;
        try {
            res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                List<PoundOrder>  truckTransports = poundMapper.findPounds(dto);
                return truckTransports;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }

    @Override
    public List<PoundUpdateOrder> findPoundsUp(PoundUpdateDto dto) {
        RLock lock = redissonClient.getLock("Pound"+3);
        boolean res = false;
        try {
            res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                List<PoundUpdateOrder>  poundUppdateOrder = poundMapper.findUpdate(dto);
                return poundUppdateOrder;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }

    @Override
    public List<PoundUpdateOrder> findPoundsUpAll() {
        RLock lock = redissonClient.getLock("Pound"+4);
        boolean res = false;
        try {
            res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        }catch (InterruptedException ex){
            log.error(ex.getMessage());
        }
        if (res) {
            try {
                List<PoundUpdateOrder>  poundUppdateOrder = poundMapper.findUpdateAll();
                return poundUppdateOrder;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }

}
